package study.springcloud.hystrix.controller;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;
import study.springcloud.hystrix.client.UserClient;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/circuit")
public class _03_CircuitController {

    @Autowired
    private UserClient userClient;

    @RequestMapping("/throw_ex")
    public Result throwEx() {
        log.info(">>>>>> aaa");
        userClient.getUserInfo2();
        log.info(">>>>>> bbb");
        return Results.ok();
    }

    @RequestMapping("/timeout")
    public Result timeout() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        log.info(">>>>>>  start");
        log.info(">>>>>> circuit_breaker cost time [{}] ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
        return Results.ok();
    }
}
